| Conditions | 21 |
| Total Lines | 395 |
| Code Lines | 67 |
| Lines | 395 |
| Ratio | 100 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like angular-loader.js ➔ setupModuleLoader often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | /** |
||
| 117 | View Code Duplication | function setupModuleLoader(window) { |
|
| 118 | |||
| 119 | var $injectorMinErr = minErr('$injector'); |
||
| 120 | var ngMinErr = minErr('ng'); |
||
| 121 | |||
| 122 | function ensure(obj, name, factory) { |
||
| 123 | return obj[name] || (obj[name] = factory()); |
||
| 124 | } |
||
| 125 | |||
| 126 | var angular = ensure(window, 'angular', Object); |
||
| 127 | |||
| 128 | // We need to expose `angular.$$minErr` to modules such as `ngResource` that reference it during bootstrap |
||
| 129 | angular.$$minErr = angular.$$minErr || minErr; |
||
| 130 | |||
| 131 | return ensure(angular, 'module', function() { |
||
| 132 | /** @type {Object.<string, angular.Module>} */ |
||
| 133 | var modules = {}; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @ngdoc function |
||
| 137 | * @name angular.module |
||
| 138 | * @module ng |
||
| 139 | * @description |
||
| 140 | * |
||
| 141 | * The `angular.module` is a global place for creating, registering and retrieving Angular |
||
| 142 | * modules. |
||
| 143 | * All modules (angular core or 3rd party) that should be available to an application must be |
||
| 144 | * registered using this mechanism. |
||
| 145 | * |
||
| 146 | * Passing one argument retrieves an existing {@link angular.Module}, |
||
| 147 | * whereas passing more than one argument creates a new {@link angular.Module} |
||
| 148 | * |
||
| 149 | * |
||
| 150 | * # Module |
||
| 151 | * |
||
| 152 | * A module is a collection of services, directives, controllers, filters, and configuration information. |
||
| 153 | * `angular.module` is used to configure the {@link auto.$injector $injector}. |
||
| 154 | * |
||
| 155 | * ```js |
||
| 156 | * // Create a new module |
||
| 157 | * var myModule = angular.module('myModule', []); |
||
| 158 | * |
||
| 159 | * // register a new service |
||
| 160 | * myModule.value('appName', 'MyCoolApp'); |
||
| 161 | * |
||
| 162 | * // configure existing services inside initialization blocks. |
||
| 163 | * myModule.config(['$locationProvider', function($locationProvider) { |
||
| 164 | * // Configure existing providers |
||
| 165 | * $locationProvider.hashPrefix('!'); |
||
| 166 | * }]); |
||
| 167 | * ``` |
||
| 168 | * |
||
| 169 | * Then you can create an injector and load your modules like this: |
||
| 170 | * |
||
| 171 | * ```js |
||
| 172 | * var injector = angular.injector(['ng', 'myModule']) |
||
| 173 | * ``` |
||
| 174 | * |
||
| 175 | * However it's more likely that you'll just use |
||
| 176 | * {@link ng.directive:ngApp ngApp} or |
||
| 177 | * {@link angular.bootstrap} to simplify this process for you. |
||
| 178 | * |
||
| 179 | * @param {!string} name The name of the module to create or retrieve. |
||
| 180 | * @param {!Array.<string>=} requires If specified then new module is being created. If |
||
| 181 | * unspecified then the module is being retrieved for further configuration. |
||
| 182 | * @param {Function=} configFn Optional configuration function for the module. Same as |
||
| 183 | * {@link angular.Module#config Module#config()}. |
||
| 184 | * @returns {angular.Module} new module with the {@link angular.Module} api. |
||
| 185 | */ |
||
| 186 | return function module(name, requires, configFn) { |
||
| 187 | |||
| 188 | var info = {}; |
||
| 189 | |||
| 190 | var assertNotHasOwnProperty = function(name, context) { |
||
| 191 | if (name === 'hasOwnProperty') { |
||
| 192 | throw ngMinErr('badname', 'hasOwnProperty is not a valid {0} name', context); |
||
| 193 | } |
||
| 194 | }; |
||
| 195 | |||
| 196 | assertNotHasOwnProperty(name, 'module'); |
||
| 197 | if (requires && modules.hasOwnProperty(name)) { |
||
| 198 | modules[name] = null; |
||
| 199 | } |
||
| 200 | return ensure(modules, name, function() { |
||
| 201 | if (!requires) { |
||
| 202 | throw $injectorMinErr('nomod', 'Module \'{0}\' is not available! You either misspelled ' + |
||
| 203 | 'the module name or forgot to load it. If registering a module ensure that you ' + |
||
| 204 | 'specify the dependencies as the second argument.', name); |
||
| 205 | } |
||
| 206 | |||
| 207 | /** @type {!Array.<Array.<*>>} */ |
||
| 208 | var invokeQueue = []; |
||
| 209 | |||
| 210 | /** @type {!Array.<Function>} */ |
||
| 211 | var configBlocks = []; |
||
| 212 | |||
| 213 | /** @type {!Array.<Function>} */ |
||
| 214 | var runBlocks = []; |
||
| 215 | |||
| 216 | var config = invokeLater('$injector', 'invoke', 'push', configBlocks); |
||
| 217 | |||
| 218 | /** @type {angular.Module} */ |
||
| 219 | var moduleInstance = { |
||
| 220 | // Private state |
||
| 221 | _invokeQueue: invokeQueue, |
||
| 222 | _configBlocks: configBlocks, |
||
| 223 | _runBlocks: runBlocks, |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @ngdoc method |
||
| 227 | * @name angular.Module#info |
||
| 228 | * @module ng |
||
| 229 | * |
||
| 230 | * @param {Object=} info Information about the module |
||
| 231 | * @returns {Object|Module} The current info object for this module if called as a getter, |
||
| 232 | * or `this` if called as a setter. |
||
| 233 | * |
||
| 234 | * @description |
||
| 235 | * Read and write custom information about this module. |
||
| 236 | * For example you could put the version of the module in here. |
||
| 237 | * |
||
| 238 | * ```js |
||
| 239 | * angular.module('myModule', []).info({ version: '1.0.0' }); |
||
| 240 | * ``` |
||
| 241 | * |
||
| 242 | * The version could then be read back out by accessing the module elsewhere: |
||
| 243 | * |
||
| 244 | * ``` |
||
| 245 | * var version = angular.module('myModule').info().version; |
||
| 246 | * ``` |
||
| 247 | * |
||
| 248 | * You can also retrieve this information during runtime via the |
||
| 249 | * {@link $injector#modules `$injector.modules`} property: |
||
| 250 | * |
||
| 251 | * ```js |
||
| 252 | * var version = $injector.modules['myModule'].info().version; |
||
| 253 | * ``` |
||
| 254 | */ |
||
| 255 | info: function(value) { |
||
| 256 | if (isDefined(value)) { |
||
| 257 | if (!isObject(value)) throw ngMinErr('aobj', 'Argument \'{0}\' must be an object', 'value'); |
||
| 258 | info = value; |
||
| 259 | return this; |
||
| 260 | } |
||
| 261 | return info; |
||
| 262 | }, |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @ngdoc property |
||
| 266 | * @name angular.Module#requires |
||
| 267 | * @module ng |
||
| 268 | * |
||
| 269 | * @description |
||
| 270 | * Holds the list of modules which the injector will load before the current module is |
||
| 271 | * loaded. |
||
| 272 | */ |
||
| 273 | requires: requires, |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @ngdoc property |
||
| 277 | * @name angular.Module#name |
||
| 278 | * @module ng |
||
| 279 | * |
||
| 280 | * @description |
||
| 281 | * Name of the module. |
||
| 282 | */ |
||
| 283 | name: name, |
||
| 284 | |||
| 285 | |||
| 286 | /** |
||
| 287 | * @ngdoc method |
||
| 288 | * @name angular.Module#provider |
||
| 289 | * @module ng |
||
| 290 | * @param {string} name service name |
||
| 291 | * @param {Function} providerType Construction function for creating new instance of the |
||
| 292 | * service. |
||
| 293 | * @description |
||
| 294 | * See {@link auto.$provide#provider $provide.provider()}. |
||
| 295 | */ |
||
| 296 | provider: invokeLaterAndSetModuleName('$provide', 'provider'), |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @ngdoc method |
||
| 300 | * @name angular.Module#factory |
||
| 301 | * @module ng |
||
| 302 | * @param {string} name service name |
||
| 303 | * @param {Function} providerFunction Function for creating new instance of the service. |
||
| 304 | * @description |
||
| 305 | * See {@link auto.$provide#factory $provide.factory()}. |
||
| 306 | */ |
||
| 307 | factory: invokeLaterAndSetModuleName('$provide', 'factory'), |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @ngdoc method |
||
| 311 | * @name angular.Module#service |
||
| 312 | * @module ng |
||
| 313 | * @param {string} name service name |
||
| 314 | * @param {Function} constructor A constructor function that will be instantiated. |
||
| 315 | * @description |
||
| 316 | * See {@link auto.$provide#service $provide.service()}. |
||
| 317 | */ |
||
| 318 | service: invokeLaterAndSetModuleName('$provide', 'service'), |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @ngdoc method |
||
| 322 | * @name angular.Module#value |
||
| 323 | * @module ng |
||
| 324 | * @param {string} name service name |
||
| 325 | * @param {*} object Service instance object. |
||
| 326 | * @description |
||
| 327 | * See {@link auto.$provide#value $provide.value()}. |
||
| 328 | */ |
||
| 329 | value: invokeLater('$provide', 'value'), |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @ngdoc method |
||
| 333 | * @name angular.Module#constant |
||
| 334 | * @module ng |
||
| 335 | * @param {string} name constant name |
||
| 336 | * @param {*} object Constant value. |
||
| 337 | * @description |
||
| 338 | * Because the constants are fixed, they get applied before other provide methods. |
||
| 339 | * See {@link auto.$provide#constant $provide.constant()}. |
||
| 340 | */ |
||
| 341 | constant: invokeLater('$provide', 'constant', 'unshift'), |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @ngdoc method |
||
| 345 | * @name angular.Module#decorator |
||
| 346 | * @module ng |
||
| 347 | * @param {string} name The name of the service to decorate. |
||
| 348 | * @param {Function} decorFn This function will be invoked when the service needs to be |
||
| 349 | * instantiated and should return the decorated service instance. |
||
| 350 | * @description |
||
| 351 | * See {@link auto.$provide#decorator $provide.decorator()}. |
||
| 352 | */ |
||
| 353 | decorator: invokeLaterAndSetModuleName('$provide', 'decorator', configBlocks), |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @ngdoc method |
||
| 357 | * @name angular.Module#animation |
||
| 358 | * @module ng |
||
| 359 | * @param {string} name animation name |
||
| 360 | * @param {Function} animationFactory Factory function for creating new instance of an |
||
| 361 | * animation. |
||
| 362 | * @description |
||
| 363 | * |
||
| 364 | * **NOTE**: animations take effect only if the **ngAnimate** module is loaded. |
||
| 365 | * |
||
| 366 | * |
||
| 367 | * Defines an animation hook that can be later used with |
||
| 368 | * {@link $animate $animate} service and directives that use this service. |
||
| 369 | * |
||
| 370 | * ```js |
||
| 371 | * module.animation('.animation-name', function($inject1, $inject2) { |
||
| 372 | * return { |
||
| 373 | * eventName : function(element, done) { |
||
| 374 | * //code to run the animation |
||
| 375 | * //once complete, then run done() |
||
| 376 | * return function cancellationFunction(element) { |
||
| 377 | * //code to cancel the animation |
||
| 378 | * } |
||
| 379 | * } |
||
| 380 | * } |
||
| 381 | * }) |
||
| 382 | * ``` |
||
| 383 | * |
||
| 384 | * See {@link ng.$animateProvider#register $animateProvider.register()} and |
||
| 385 | * {@link ngAnimate ngAnimate module} for more information. |
||
| 386 | */ |
||
| 387 | animation: invokeLaterAndSetModuleName('$animateProvider', 'register'), |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @ngdoc method |
||
| 391 | * @name angular.Module#filter |
||
| 392 | * @module ng |
||
| 393 | * @param {string} name Filter name - this must be a valid angular expression identifier |
||
| 394 | * @param {Function} filterFactory Factory function for creating new instance of filter. |
||
| 395 | * @description |
||
| 396 | * See {@link ng.$filterProvider#register $filterProvider.register()}. |
||
| 397 | * |
||
| 398 | * <div class="alert alert-warning"> |
||
| 399 | * **Note:** Filter names must be valid angular {@link expression} identifiers, such as `uppercase` or `orderBy`. |
||
| 400 | * Names with special characters, such as hyphens and dots, are not allowed. If you wish to namespace |
||
| 401 | * your filters, then you can use capitalization (`myappSubsectionFilterx`) or underscores |
||
| 402 | * (`myapp_subsection_filterx`). |
||
| 403 | * </div> |
||
| 404 | */ |
||
| 405 | filter: invokeLaterAndSetModuleName('$filterProvider', 'register'), |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @ngdoc method |
||
| 409 | * @name angular.Module#controller |
||
| 410 | * @module ng |
||
| 411 | * @param {string|Object} name Controller name, or an object map of controllers where the |
||
| 412 | * keys are the names and the values are the constructors. |
||
| 413 | * @param {Function} constructor Controller constructor function. |
||
| 414 | * @description |
||
| 415 | * See {@link ng.$controllerProvider#register $controllerProvider.register()}. |
||
| 416 | */ |
||
| 417 | controller: invokeLaterAndSetModuleName('$controllerProvider', 'register'), |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @ngdoc method |
||
| 421 | * @name angular.Module#directive |
||
| 422 | * @module ng |
||
| 423 | * @param {string|Object} name Directive name, or an object map of directives where the |
||
| 424 | * keys are the names and the values are the factories. |
||
| 425 | * @param {Function} directiveFactory Factory function for creating new instance of |
||
| 426 | * directives. |
||
| 427 | * @description |
||
| 428 | * See {@link ng.$compileProvider#directive $compileProvider.directive()}. |
||
| 429 | */ |
||
| 430 | directive: invokeLaterAndSetModuleName('$compileProvider', 'directive'), |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @ngdoc method |
||
| 434 | * @name angular.Module#component |
||
| 435 | * @module ng |
||
| 436 | * @param {string} name Name of the component in camel-case (i.e. myComp which will match as my-comp) |
||
| 437 | * @param {Object} options Component definition object (a simplified |
||
| 438 | * {@link ng.$compile#directive-definition-object directive definition object}) |
||
| 439 | * |
||
| 440 | * @description |
||
| 441 | * See {@link ng.$compileProvider#component $compileProvider.component()}. |
||
| 442 | */ |
||
| 443 | component: invokeLaterAndSetModuleName('$compileProvider', 'component'), |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @ngdoc method |
||
| 447 | * @name angular.Module#config |
||
| 448 | * @module ng |
||
| 449 | * @param {Function} configFn Execute this function on module load. Useful for service |
||
| 450 | * configuration. |
||
| 451 | * @description |
||
| 452 | * Use this method to register work which needs to be performed on module loading. |
||
| 453 | * For more about how to configure services, see |
||
| 454 | * {@link providers#provider-recipe Provider Recipe}. |
||
| 455 | */ |
||
| 456 | config: config, |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @ngdoc method |
||
| 460 | * @name angular.Module#run |
||
| 461 | * @module ng |
||
| 462 | * @param {Function} initializationFn Execute this function after injector creation. |
||
| 463 | * Useful for application initialization. |
||
| 464 | * @description |
||
| 465 | * Use this method to register work which should be performed when the injector is done |
||
| 466 | * loading all modules. |
||
| 467 | */ |
||
| 468 | run: function(block) { |
||
| 469 | runBlocks.push(block); |
||
| 470 | return this; |
||
| 471 | } |
||
| 472 | }; |
||
| 473 | |||
| 474 | if (configFn) { |
||
| 475 | config(configFn); |
||
| 476 | } |
||
| 477 | |||
| 478 | return moduleInstance; |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @param {string} provider |
||
| 482 | * @param {string} method |
||
| 483 | * @param {String=} insertMethod |
||
| 484 | * @returns {angular.Module} |
||
| 485 | */ |
||
| 486 | function invokeLater(provider, method, insertMethod, queue) { |
||
| 487 | if (!queue) queue = invokeQueue; |
||
| 488 | return function() { |
||
| 489 | queue[insertMethod || 'push']([provider, method, arguments]); |
||
| 490 | return moduleInstance; |
||
| 491 | }; |
||
| 492 | } |
||
| 493 | |||
| 494 | /** |
||
| 495 | * @param {string} provider |
||
| 496 | * @param {string} method |
||
| 497 | * @returns {angular.Module} |
||
| 498 | */ |
||
| 499 | function invokeLaterAndSetModuleName(provider, method, queue) { |
||
| 500 | if (!queue) queue = invokeQueue; |
||
| 501 | return function(recipeName, factoryFunction) { |
||
| 502 | if (factoryFunction && isFunction(factoryFunction)) factoryFunction.$$moduleName = name; |
||
| 503 | queue.push([provider, method, arguments]); |
||
| 504 | return moduleInstance; |
||
| 505 | }; |
||
| 506 | } |
||
| 507 | }); |
||
| 508 | }; |
||
| 509 | }); |
||
| 510 | |||
| 511 | } |
||
| 512 | |||
| 534 |